package wayden;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.HashMap;
import java.util.Map;



import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class Profile extends ActionSupport {
    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private String id;
	private String id2;
	
    public String getId2() {
		return id2;
	}

	public void setId2(String id2) {
		this.id2 = id2;
	}

	private String idUser;
    private String about;
    private String logo;
    private String name;
    Map session;
    public String profile() throws ClassNotFoundException, SQLException {
	session = ActionContext.getContext().getSession();
	String user;
	if (session.get("user") != null) {
	if (session.get("user2") != null) user = "user2"; 
	else user = "user";
	setId(session.get("user").toString());
	String q_profile = "select * from profile where id = " + //+ getId();
		session.get(user).toString();
	String q_user = "select * from users where id = ?";
	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();
	Statement st = conn.createStatement();
	PreparedStatement ps;
	st.executeQuery(q_profile);
	ResultSet rs = st.getResultSet();
	if (session.containsKey("user2"))
	this.id2 = session.get("user2").toString();
	System.err.println("*******_______"+getId2());
	session.remove("user2");
	if (rs.next()) {
	    ps = conn.prepareStatement(q_user);

	    this.id = rs.getString(1);
	    
	    
	    this.idUser = rs.getString(2);
	    this.about = rs.getString(3);
	    this.logo ="upload/"+ rs.getString(4);
	    System.err.println(logo);
	    ps.setString(1, idUser);
	    ps.executeQuery();
	    rs = ps.getResultSet();
	    rs.next();
	    this.name = rs.getString(3);
	   // System.err.println(session.get("user").toString());
	    
	}
	//session.put("name1", "kolin");
	//System.err.println(session.get("name1").toString());
	return "success";
    }
	else return "error";
    }
    
    public String save() throws ClassNotFoundException, SQLException {
    session = ActionContext.getContext().getSession();
    //session.put("user", getId());
    //session.get("name1");
    
	//System.err.println(session.get("user").toString());
    String insert = "update profile set about=?, logo=? where id_user = ?";
	
	Connection conn = null;
	ConnectDB connectDB = new ConnectDB();
	conn = connectDB.Connect();

	String about = getAbout();

	//String logo = new String("");
	//logo = "/upload/"+getLogo();
	System.err.println(logo);
	String id_user = getId();

	PreparedStatement ps;
	ps = conn.prepareStatement(insert);
	ps.setString(1, about);
	if (logo == null)
	    ps.setString(2, "");
	else
	    ps.setString(2, logo);
	ps.setString(3, id_user);
	ps.execute();
	
	
	
	//System.err.println(getId());
	
	return "success";
    }

    public void setId(String id) {

	this.id = id;

    }

    public String getId() {
	return this.id;
    }

    public void setName(String name) {

	this.name = name;

    }

    public String getName() {
	return this.name;
    }

    public void setAbout(String about) {

	this.about = about;

    }

    public String getAbout() {
	return this.about;
    }

    public void setLogo(String logo) {

	this.logo = logo;

    }

    public String getLogo() {
	return this.logo;
    }

    public String getIdUser() {
	return this.idUser;
    }

    public void setIdUser(String idUser) {

	this.idUser = idUser;

    }

}
